import inquirer from "inquirer";
import {  plugins } from "./data";
import { pluginUtils } from "./utils";
//
import {QS,getPluginSortInfo,dictPlugDemos} from "./questions";
//

const QD = QS.D;
const QP = QS.plugin
const QG = QS.pluginGroup
const QE = QS.pluginDemo
//
const addPlugin = async () => {
  let {groupName} = await inquirer.prompt([QP.group]);
  if(groupName == "new"){
    const groupData = await addPluginGroup();
    groupName = groupData.name;
  }
  let {name,label,desc} = await inquirer.prompt([QP.name,QD.label,QD.desc])
  //
  let {sort} = await inquirer.prompt([{...QD.sort,...getPluginSortInfo(groupName)}])
  const {menu} = await inquirer.prompt([{...QD.menu,default:label}])
  const {isAddDemo} = await inquirer.prompt([QD.isAddDemo])
  const data = {name,label,desc,sort,menu,group:groupName};
  pluginUtils.add(data,isAddDemo)
  if(isAddDemo){
    await addPluginDemo(data)
  }
  return data;
}

const delPlugin = async () => {
  const { name,isDelete } = await inquirer.prompt([QP.select,QD.isDelete]);
  if(isDelete){
    pluginUtils.del(name)
  }
}
const addPluginGroup = async () => {
  const { parent } = await inquirer.prompt([QG.parent]);
  //
  const {name,label,desc} = await inquirer.prompt([QG.name,QD.label,QD.desc])
  const {sort} = await inquirer.prompt([{...QD.sort,...getPluginSortInfo(parent,true)}])
  const data = {name,label,desc,parent,sort}
  pluginUtils.addGroup(data)
  return data
}
const delPluginGroup = async () => {
  const { name,isDelete } = await inquirer.prompt([QG.select,QD.isDelete]);
  if(isDelete){
    pluginUtils.delGroup(name)
  }
}
const addPluginDemo = async (d) => {
  let t = d;
  if(!d){
    const { name:n } = await inquirer.prompt([QP.select]);
    t = plugins[n]
  }
  const {name,label,desc} = await inquirer.prompt([QE.name,QD.label,QD.desc])
  const {sort} = await inquirer.prompt([{...QD.sort,default:t.demos?.length || 0}])
  //
  const data = {name,label,desc,upper:t.name,group:t.group,sort}
  //
  pluginUtils.addDemo(data)
  //
  return data;
}
const delPluginDemo = async () => {
  const { name:n } = await inquirer.prompt([QP.select]);
  const choices = dictPlugDemos.filter(d => plugins[n].demos.includes(d.value))
  const { name,isDelete } = await inquirer.prompt([{...QE.select,choices},QD.isDelete]);
  if(isDelete){
    pluginUtils.delDemo(name)
  }
}

const refreshPluginData = async () => {
  const { name:n } = await inquirer.prompt([QP.select]);
  if(n){
    pluginUtils.refreshMain(plugins[n]);
  }
}
const refreshPluginDocs = async () => {
  const { name:n } = await inquirer.prompt([QP.select]);
  if(n){
    pluginUtils.refreshDocs(plugins[n]);
  }
}
const refreshPluginMd = async () => {
  const { name:n } = await inquirer.prompt([QP.select]);
  if(n){
    pluginUtils.refreshMd(plugins[n]);
  }
}

const refreshPluginDemos = async () => {
  const { name:n } = await inquirer.prompt([QP.select]);
  if(n){
    pluginUtils.refreshDemos(plugins[n]);
  }
}

const refreshPlugin = async () => {
  const { name:n } = await inquirer.prompt([QP.select]);
  if(n){
    pluginUtils.refresh(plugins[n]);
  }
}

const refreshPluginsIndex = async () => {
  const { isReset } = await inquirer.prompt([QD.isReset]);
  if(isReset){
    pluginUtils.refreshIndex()
  }
}

export default {
  addPlugin,
  delPlugin,
  addPluginGroup,
  delPluginGroup,
  addPluginDemo,
  delPluginDemo,
  refreshPluginData,
  refreshPluginMd,
  refreshPluginDemos,
  refreshPluginDocs,
  refreshPluginsIndex,
  refreshPlugin
}
